import { assign } from 'lodash'
import { fetchTemplates, fetchTableHeaders, fetchTableFilters, fetchTables } from '@/api/templateDetail'

const defaultState = () => {
  return {
    tableData: null,
    tableLoading: false,
    tableHint: '',
    tablePageParams: {
      pageNum: 1,
      pageSize: 20
    },
    total: 0,
    templateList: [],
    tableHeaderList: [],
    searchList: [],
    selecteTemplateId: undefined,
    showTable: false
  }
}

export default {
  namespaced: true,
  name: 'templateDetail',
  state: defaultState(),
  mutations: {
    REST_STATE(state) {
      state = assign(state, defaultState())
    },
    CHANGE_TEMPLATE_LIST(state, list) {
      state.templateList = list
    },
    CHANGE_HEADER_LIST(state, list) {
      state.tableHeaderList = list
    },
    CHANGE_SEARCH_LIST(state, list) {
      state.searchList = list
    },
    SET_TABLE_PAGE_PARAMS(state, data) {
      state.tablePageParams = {
        ...state.tablePageParams,
        ...data
      }
    },
    SET_TABLE_LOADING(state, flag) {
      state.tableLoading = flag
    },
    SET_TABLE_DATA(state, tableData) {
      state.tableData = tableData
    },
    SET_TABLE_TOTAL(state, num) {
      state.total = num
    },
    SET_TABLE_HINT(state, val) {
      state.tableHint = val
    },
    SET_SELECTE_TEMPLATE_ID(state, id) {
      state.selecteTemplateId = id
    },
    SET_SHOW_TABLE(state, flag) {
      state.showTable = flag
    }
  },
  actions: {
    clearStore(ctx) {
      ctx.commit('REST_STATE')
    },
    async getTemplates(ctx) {
      const { code, date } = await fetchTemplates()
      if (code === 200) {
        ctx.commit('CHANGE_TEMPLATE_LIST', date)
      } else {
        ctx.commit('CHANGE_TEMPLATE_LIST', [])
      }
    },
    async getHeaders(ctx, params) {
      const { code, date } = await fetchTableHeaders({
        templateId: ctx.state.selecteTemplateId,
        ...params
      })
      if (code === 200) {
        ctx.commit('CHANGE_HEADER_LIST', date)
      } else {
        ctx.commit('CHANGE_HEADER_LIST', [])
      }
    },
    async getFilters(ctx, params) {
      const { code, date } = await fetchTableFilters({
        templateId: ctx.state.selecteTemplateId,
        ...params
      })
      if (code === 200) {
        ctx.commit('CHANGE_SEARCH_LIST', date)
      } else {
        ctx.commit('CHANGE_SEARCH_LIST', [])
      }
    },
    async getTables(ctx, params) {
      ctx.commit('SET_TABLE_LOADING', true)
      ctx.commit('SET_TABLE_HINT', '')
      const { code, date } = await fetchTables({
        templateId: ctx.state.selecteTemplateId,
        ...ctx.state.tablePageParams,
        ...params
      })
      ctx.commit('SET_TABLE_LOADING', false)
      if (code === 200) {
        const { list, total, pageSize, pageNum } = date
        ctx.commit('SET_TABLE_PAGE_PARAMS', {
          pageNum,
          pageSize
        })
        ctx.commit('SET_TABLE_TOTAL', total)
        ctx.commit('SET_TABLE_DATA', list)
        if (list.length === 0) {
          ctx.commit('SET_TABLE_HINT', '暂无数据')
        }
      }
    },
    changeTemplateId(ctx, id) {
      ctx.commit('SET_SHOW_TABLE', true)
      ctx.commit('SET_SELECTE_TEMPLATE_ID', id)
    },
    changeTablePageParams(ctx, data) {
      ctx.commit('SET_TABLE_PAGE_PARAMS', data)
    }
  }
}
